package com.xuecheng.media.service.impl;

import com.xuecheng.media.mapper.MediaFilesMapper;
import com.xuecheng.media.mapper.MediaProcessHistoryMapper;
import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.model.po.MediaProcessHistory;
import com.xuecheng.media.service.MediaProcessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 冯佳鑫 又菜又爱学
 * @version 1.0
 * @DATE now()
 */
@Service
public class MediaProcessServiceimpl implements MediaProcessService {
    @Autowired
    MediaProcessMapper mediaProcessMapper;
    @Autowired
    MediaFilesMapper mediaFilesMapper;
    @Autowired
    MediaProcessHistoryMapper mediaProcessHistoryMapper;
    @Autowired
    MediaProcessService mediaProcessService;
    @Override
    public List<MediaProcess> selectListByShardIndex(int shardTotal, int shardIndex, int count) {
        List<MediaProcess> mediaProcesses = mediaProcessMapper.selectListByShardIndex(shardTotal, shardIndex, count);
        return mediaProcesses;
    }

    /**
     * 开启执行任务
     * @param id
     * @return
     */
    @Override
    public boolean startTask(Long id) {
        int i = mediaProcessMapper.startTask(id);
        return i>0?true:false;
    }

    /**
     * 保存任务结果
     * @param taskId
     * @param status
     * @param fileId
     * @param url
     * @param errorMsg
     */
    @Override
    public void saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg) {
        //要保存的任务
        MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
        //任务为空，退出============
        if (mediaProcess == null){
            return;
        }
        //如果任务执行失败============
        if (status.equals("3")) {
            //更新mediaProcess表
            int i = mediaProcess.getFailCount() + 1;
            mediaProcess.setStatus("3");
            mediaProcess.setFailCount(i);
            mediaProcess.setErrormsg(errorMsg);
            mediaProcessMapper.updateById(mediaProcess);
            return;
        }
        //如果任务执行成功==========
        //更新mediaProcess表
        mediaProcess.setStatus("2");
        mediaProcess.setUrl(url);
        mediaProcess.setCreateDate(LocalDateTime.now());
        mediaProcessMapper.updateById(mediaProcess);
       //更新mediafiles表
        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
        if (mediaFiles!=null){
            mediaFiles.setUrl(url);
            mediaFilesMapper.updateById(mediaFiles);
        }
        //将mediaProcess表插入到mediaProcessHistory表中
        MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
        BeanUtils.copyProperties(mediaProcess,mediaProcessHistory);
        mediaProcessHistoryMapper.insert(mediaProcessHistory);
        //删除mediaProcess表
        mediaProcessMapper.deleteById(mediaProcess.getId());
    }
}
